package com.cmcc.linlaker.ui.mine.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.VolleyError;
import com.cmcc.linlaker.R;
import com.cmcc.linlaker.http.ApiDefine;
import com.cmcc.linlaker.ui.mine.adapter.BookAdapter;
import com.cmcc.linlaker.ui.mine.adapter.BookPageAdapter;
import com.cmcc.linlaker.ui.mine.bean.BookBean;
import com.cmcc.linlaker.ui.mine.bean.BookTypeBean;
import com.cmcc.linlaker.utils.IntentUtils;
import com.cmcc.linlaker.volley.GsonRequestHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BookFragment extends Fragment {
    private View fragmentView;
    private BookTypeBean.BookTypeListBean typeListBean;

    public static BookFragment newInstance(BookTypeBean.BookTypeListBean typeListBean) {
        BookFragment fragment = new BookFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("typeListBean", typeListBean);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fragmentView = inflater.inflate(R.layout.fragment_book, container, false);

        typeListBean = (BookTypeBean.BookTypeListBean) getArguments().getSerializable("typeListBean");

        initView();

        return fragmentView;
    }

    private RecyclerView recycler_book;
    private BookAdapter adapter;
    private List<BookBean.BookListBean> itemList = new ArrayList<>();


    private void initView() {
        recycler_book = fragmentView.findViewById(R.id.recycler_book);
        setRecyclerView(recycler_book, itemList);

        getBookList();
    }


    @Override
    public void onResume() {
        super.onResume();
    }

    private void setRecyclerView(final RecyclerView recycler_view, final List<BookBean.BookListBean> itemList) {
        recycler_view.setHasFixedSize(true);
        recycler_view.setLayoutManager(new LinearLayoutManager(getActivity()));

        recycler_view.setNestedScrollingEnabled(false);
        adapter = new BookAdapter(getActivity(), itemList);
        recycler_view.setAdapter(adapter);
    }

    /**
     * 知识库列表
     */
    private void getBookList() {
        GsonRequestHelper helper = new GsonRequestHelper(getActivity());
        Map<String, String> params = new HashMap<>();
        params.put("wordTypeId", typeListBean.id);

        helper.sendPOSTRequest(ApiDefine.BOOK_LIST, BookBean.class, params, false, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
                if (data != null) {
                    BookBean bean = (BookBean) data;

                    if (bean.code.equals("0")) {
                        List<BookBean.BookListBean> list = bean.rows;

                        itemList.clear();
                        itemList.addAll(list);
                        adapter.notifyDataSetChanged();
                    } else if (bean.code.equals(IntentUtils.VAILD_CODE)) {
                        IntentUtils.skipToLogin(getActivity());
                    }
                }
            }
        });
    }

}
